package com.ruoyi.train.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.train.service.IETrainService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.e_train.ETrainRecord;
import com.ruoyi.train.service.IETrainRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训人员Controller
 * 
 * @author JYW
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/train/record")
public class ETrainRecordController extends BaseController
{
    @Autowired
    private IETrainRecordService eTrainRecordService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IETrainService eTrainService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询培训人员列表
     */
    @PreAuthorize("@ss.hasPermi('train:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ETrainRecord eTrainRecord)
    {
        startPage();
        List<ETrainRecord> list = eTrainRecordService.selectETrainRecordList(eTrainRecord);
        return getDataTable(list);
    }

    /**
     * 查询培训人员列表
     */
    @PreAuthorize("@ss.hasPermi('train:record:list')")
    @GetMapping("/listDetail")
    public TableDataInfo listDetail(ETrainRecord eTrainRecord)
    {
        startPage();
        List<ETrainRecord> list = eTrainRecordService.selectETrainRecordList(eTrainRecord);
        List<Map<String,Object>> mapList = new ArrayList<>();
        list = list.stream().filter(eTrainRecord1 ->eTrainRecord1.getApplyStatus() != null && eTrainRecord1.getApplyStatus().equals("1")).collect(Collectors.toList());
        if (!list.isEmpty()){
                    list.forEach(eTrainRecord1 -> {
                Map<String,Object> map = new HashMap<>();
                SysUser sysUser = sysUserService.selectUserById(eTrainRecord1.getuId());
                map.put("tName",eTrainService.selectETrainById(eTrainRecord1.gettId()).getName());
                map.put("uName",sysUser.getUserName());
                map.put("deptName",sysDeptService.selectDeptById(sysUser.getDeptId()).getDeptName());
                map.put("trainRecord",eTrainRecord1);
                mapList.add(map);
            });
        }
        return getDataTable(mapList);
    }


    /**
     * 查询培训申请人员列表
     */
    @PreAuthorize("@ss.hasPermi('train:record:list')")
    @GetMapping("/listTrailDetail")
    public TableDataInfo listTrailDetail(ETrainRecord eTrainRecord)
    {
        startPage();
        List<ETrainRecord> list = eTrainRecordService.selectETrainRecordList(eTrainRecord);
        List<Map<String,Object>> mapList = new ArrayList<>();
        if (!list.isEmpty()){
            list.forEach(eTrainRecord1 -> {
                Map<String,Object> map = new HashMap<>();
                SysUser sysUser = sysUserService.selectUserById(eTrainRecord1.getuId());
                map.put("tName",eTrainService.selectETrainById(eTrainRecord1.gettId()).getName());
                map.put("uName",sysUser.getUserName());
                map.put("deptName",sysDeptService.selectDeptById(sysUser.getDeptId()).getDeptName());
                map.put("trainRecord",eTrainRecord1);
                mapList.add(map);
            });
        }
        return getDataTable(mapList);
    }

    /**
     * 导出培训人员列表
     */
    @PreAuthorize("@ss.hasPermi('train:record:export')")
    @Log(title = "培训人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ETrainRecord eTrainRecord)
    {
        List<ETrainRecord> list = eTrainRecordService.selectETrainRecordList(eTrainRecord);
        ExcelUtil<ETrainRecord> util = new ExcelUtil<ETrainRecord>(ETrainRecord.class);
        util.exportExcel(response, list, "培训人员数据");
    }

    /**
     * 获取培训人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('train:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eTrainRecordService.selectETrainRecordById(id));
    }

    /**
     * 新增培训人员
     */
    @PreAuthorize("@ss.hasPermi('train:record:add')")
    @Log(title = "培训人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ETrainRecord eTrainRecord)
    {
        return toAjax(eTrainRecordService.insertETrainRecord(eTrainRecord));
    }

    /**
     * 新增培训申请
     */
//    @PreAuthorize("@ss.hasPermi('train:record:add')")
    @Log(title = "培训申请", businessType = BusinessType.INSERT)
    @PostMapping("/apply")
    public AjaxResult apply(@RequestBody ETrainRecord eTrainRecord)
    {
        Long userId = SecurityUtils.getUserId();
        eTrainRecord.setuId(userId);
        if (!eTrainRecordService.selectETrainRecordList(eTrainRecord).isEmpty()){
            throw new BadCredentialsException("您已申请该培训");
        }
        return toAjax(eTrainRecordService.insertETrainRecord(eTrainRecord));
    }



    /**
     * 修改培训人员
     */
    @PreAuthorize("@ss.hasPermi('train:record:edit')")
    @Log(title = "培训人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ETrainRecord eTrainRecord)
    {
        return toAjax(eTrainRecordService.updateETrainRecord(eTrainRecord));
    }

    /**
     * 删除培训人员
     */
    @PreAuthorize("@ss.hasPermi('train:record:remove')")
    @Log(title = "培训人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eTrainRecordService.deleteETrainRecordByIds(ids));
    }
}
